<?
/*

this will take a form element id
and look it up and display the details

*/

	include_once("cmsConfig.php");




	if($authInfo->usertype != '0') {
		die("FORBIDDEN");
	}
	
	
	$formElementTableWriter = new CmsTableWriter( "formelement", $db );

	$generateNew = false;
	if( array_key_exists("generateNew",$_REQUEST)) {
		$generateNew = $_REQUEST['generateNew'];
	}
	
	$fieldName = extractStringWithKeyFromArray("fieldName", $_REQUEST );	
	$elementId = extractNumberWithKeyFromArray("elementId", $_REQUEST );
	
	$elementSaved = false;

	if ($generateNew==true && isset($fieldName) && is_string($fieldName) && strlen($fieldName) > 0) {
		//echo "Into saving<Br>";
		$elementId = $formElementTableWriter->db->getId('formelementtablesequence');
		$formElementTableWriter->begin();
		
		$label = "New Form Element " . $elementId;
		
		$formElementTableWriter->addField('elementid', $elementId);
		$formElementTableWriter->addField('elementtype', 1 );
		$formElementTableWriter->addField('name', $fieldName );
		$formElementTableWriter->addField('label', $label);
		
		$elementSaved = $formElementTableWriter->newItem();
		
		if (!$elementSaved) {
			$elementId= null;
		}
	}

	if ($elementId == null || !is_numeric($elementId) || $elementId == 0) {
	
		if ($elementSaved == null) {
?>
<span class="formElementDescriptionLabel">Off</span>
<?
		} else if (!$elementSaved) {
?>
<b class="error">Error Creating New Form Element</b>
<?
		}
	} else {
		
		$formElement = $formElementTableWriter->readItem( $elementId );	
		
		if ($formElement) {
			print <<< EOD
<table class="formElementEditor" cellpadding="0" cellspacing="0"  >
EOD;
		
			foreach( $formElement as $key => $value) {

				$name = "formElement_" . $elementId . "_" . $key;				
				$varName= "formElement[".$elementId."][".$key ."]";
				switch ($key) {

					case "elementid"://not editable
					case "name": //not editable
					break;					
					default:				
						print <<< EOD
<tr>
<td class="formElementDescriptionLabel">$key</td>
<td class="formElementDescriptionSettings">
EOD;
						switch ($key) {

							case "elementtype"://drop down
				
							optionList(array("id"=>$name, "name"=>$varName, "class" => "formElementEditor"),false,null,0,1,"SELECT DISTINCT * FROM elementtype", $db);
		//doElementSelectForKeyAndValue
							$jsCallsForInit[] = "doElementSelectForKeyAndValue('$name','$value')";
							

							break;
							
							case "label"://input
								$value = magicClean($value);
								print <<< EOD
<input name="$varName" id="$name" type="text" class="formElementEditor" value="$value" />
EOD;
							break;

							case "requiredfield": //check box
								if ($value == "1") 
									$isChecked="true";
								else
									$isChecked ="false";
									
								$jsCallsForInit[] = "jsInitCheckBoxForKey('$name',$isChecked)";
								
							case "defaultvalue": //text
							case "help": 	//text
							case "sourcequery": // text
								$value=magicClean($value);
								print <<< EOD
<textarea class="formElementEditor" name="$varName" id="$name">$value</textarea>								
EOD;
							break;						
						}

						print <<< EOD
</td>
</tr>
EOD;
					break;

				}
			}
			
			print <<< EOD
</table>
<script language="javascript" type="text/javascript">
EOD;

	if ($elementSaved) {
			print <<< EOD
	newFormElementAddedForElementIdWithFieldAndLabel($elementId, '$fieldName','$label');
EOD;
	}

	foreach( $jsCallsForInit as $functionToCall) {
		echo $functionToCall .";\n";
	}
	
	print <<< EOD
</script>
EOD;
		} else {
?>
<span class="error">ELEMENT NOT FOUND</span>
<?
		}
	}
?>